<?php


namespace app\admin\controller;


use app\common\controller\AdminController;
use think\Db;
use think\Exception;

class Admin extends AdminController {

    /**
     * 权限配置
     * 1查看权限、2管理权限、4导入权限、
     * @var array
     */
    protected $levelConfig = [
        1 => ['getlist', 'info', 'wherelist'],
        2 => ['add', 'edit', 'del'],
        4 => [],
        8 => []
    ];

    public $resubmitLock = ['add', 'edit'];

    protected $notNeedAuthAction = ['personalinfo', 'editmyself'];

    /**
     * 用户详情页
     * @return false|string
     */
    public function getlist() {
        try {
            $params = $this->checkParams('');
            $page = $params['page'] = $params['page'] ?? 1;
            $params['row'] = $params['row'] ?? 10;
            $adminModel = new \app\admin\model\Admin();
            $listData = $adminModel->getList($params);
            if (!$listData) {
                return $this->resErr();
            }
            list($count, $pageNum, $data) = $listData;
            return $this->resData('success', compact('page', 'pageNum', 'count', 'data'));
        } catch (Exception $e) {
            return $this->resErr('操作失败~');
        }
    }

    /**
     * 管理员添加
     * @return false|string
     * @throws \think\Exception
     */
    public function add() {
        try {
            $params = $this->checkParams('account,nickname,role_id,pwd,status');
            $adminModel = new \app\admin\model\Admin();
            if (!$adminModel->add($params)) {
                return $this->resErr();
            }
            return $this->resData('添加成功', []);
        } catch (Exception $e) {
            return $this->resErr('操作失败~');
        }
    }

    /**
     * 管理员修改
     * @return false|string
     * @throws \think\Exception
     */
    public function edit() {
        try {
            $params = $this->checkParams('id,status');
            $adminModel = new \app\admin\model\Admin();
            if ($params['id'] == 1) return $this->resErr('禁止编辑超级管理员');
            $adminModel::where('id', $params['id'])->update(['status' => $params['status']]);
            return $this->resData('修改成功', []);
        } catch (Exception $e) {
            return $this->resErr('操作失败~');
        }
    }

    /**
     * 管理员删除
     * @return false|string
     * @throws \think\Exception
     */
    public function del() {
        try {
            $params = $this->checkParams('id');
            $adminModel = new \app\admin\model\Admin();
            if ($params['id'] == 1) {
                return $this->resErr('禁止删除超级管理员');
            }
            if (!$adminModel->del($params['id'])) {
                return $this->resErr();
            }
            return $this->resData('删除成功', []);
        } catch (Exception $e) {
            return $this->resErr('操作失败~');
        }
    }

    /**
     * 管理员详情
     * @return false|string
     * @throws \think\Exception
     */
    public function info() {
        try {
            $params = $this->checkParams('id');
            $adminModel = new \app\admin\model\Admin();
            if (!$info = $adminModel->info($params['id'])) {
                return $this->resErr('该数据不存在');
            }
            $info['utime'] = date('Y-m-d H:i:s', $info['utime']);
            $info['log_time'] = date('Y-m-d H:i:s', $info['log_time']);
            $info['status'] = $info['status'] == 1 ? '正常' : '锁定';
            return $this->resData('success', compact('info'));
        } catch (Exception $e) {
            return $this->resErr('操作失败~');
        }
    }

    /**
     * 个人信息
     * @return false|string
     */
    public function personalInfo() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $data['name'] = $this->uInfo['nickname'];
            if ($this->uInfo['level'] == '*') {
                $data['auth'] = ['*'];
            } else {
                $role_id = db('admin')->where('id', $params['user_id'])->value('role_id');
                $level = db('admin_role')->where('id', $role_id)->value('level');
                $level3 = \app\admin\model\AdminRole::levelSplit($level);//三级权限
                $controller = json_decode($level, true);
                $level2 = array_keys($controller);//二级权限
                $tree = config('level.tree');
                $relationship = config('level.relationship');//三级和二级映射关系
                $e = [];
                foreach ($tree as $k1 => $v1) {
                    if (!in_array($v1['key'], $level2)) {
                        foreach ($v1['children'] as $k2 => $v2) {
                            if (in_array($v2['key'], $level2)) {
                                $e[] = $v2['key'];
                            }
                        }
                    }
                }
                $level1 = [];
                if ($e) {
                    foreach ($e as $k3 => $v3) {
                        $level1[] = $relationship[$v3];
                    }
                }
                $level1 = array_unique($level1);//一级权限
                $merge = array_merge($level3, $level2, $level1);
                $data['auth'] = $merge;
            }
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 所属组别
     * @return false|string
     */
    public function whereList() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $list = Db::name('admin_role')
                ->field('id,name')
                ->where('status', 1)
                ->where('id', '<>', config('global.adminRole_id'))
                ->select();
            return $this->resData('success', $list);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改自己密码
     * @return false|string
     */
    public function editMyself() {
        try {
            $params = $this->checkParams('pwd,again_pwd,old_pwd');
            if ($params === false) return $this->resErr();
            $res = \app\admin\model\Admin::editMyself($params);
            if (!$res) return $this->resErr();
            else return $this->resData('修改成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }
}